﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using CVsharp.DWZMvc.Models;

namespace CVsharp.DWZMvc.Controllers
{
    public class ReviewController : Controller
    {
        public yjjDBEntities db = new yjjDBEntities();

        //
        // GET: /Review/

        public ActionResult Index(int dictid)
        {
            List<DSA_GradeConfig> model = db.DSA_GradeConfig.Where(i => i.ProjectClassification == dictid).ToList();
            ViewBag.DrugStoreSelect = db.DSA_DrugStore;
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(string[] ProjectClassification, string[] Grade, int DrugStore)
        {
            //List<DSA_Grade> model = new List<DSA_Grade>();

            for (int j = 0; j < Grade.Count(); j++)
            {
                var DSA_grade = new DSA_Grade
                            {
                                DrugStoreID = DrugStore,
                                Grade = Convert.ToInt32(Grade[j]),
                                AssessmentConfigID = Convert.ToInt32(ProjectClassification[j]),
                                Remarks = DateTime.Now.ToString()
                            };
                db.AddToDSA_Grade(DSA_grade);
            }
            //写入药房数据库最后审核日期
            DSA_DrugStore DSA_drugStore = db.DSA_DrugStore.Where(m => m.DrugStoreID == DrugStore).FirstOrDefault();
            DSA_drugStore.LastGradeDate = DateTime.Now;
            db.SaveChanges();
            var reslut = new
                             {
                                 statusCode = "200",
                                 message = "审核成功",
                             };
            return Json(reslut);
        }

        [HttpGet]
        public ActionResult List()
        {
            ViewBag.DrugStoreSelect = db.DSA_DrugStore;
            ViewBag.pageNum = 1;
            ViewBag.numPerPage = 20;
            ViewBag.keyStrings = "";
            ViewBag.keyStores = "";
            ViewBag.totalnum = db.DSA_Grade.Count();
            IQueryable<DSA_Grade> model = db.DSA_Grade.OrderBy(m => m.GradeID).Take(20);
            ViewBag.currentpagenum = model.Count();
            return View(model.ToList());
        }

        [HttpPost]
        public ActionResult List(int? keyStores, int? keyStore, string keyStrings, string keyString, int? pageNum,
                                 int? numPerPage)
        {
            //drugstoreselect.AddObject(new DSA_DrugStore() { DrugStoreID=0, Name="未选择" });
            ViewBag.DrugStoreSelect = db.DSA_DrugStore;

            if (pageNum == null) pageNum = 1;
            if (numPerPage == null) numPerPage = 20;
            keyString = keyString ?? "";
            ViewBag.keyStrings = keyString = (keyString == "" && keyStrings != null) ? keyStrings : keyString;
            ViewBag.keyStores = keyStore = (keyStore == null && keyStores != null) ? keyStores : keyStore;
            ViewBag.pageNum = pageNum.Value;
            ViewBag.numPerPage = numPerPage.Value;
            int skipnum = (pageNum.Value - 1)*numPerPage.Value;

            IQueryable<DSA_Grade> DSA_grade = db.DSA_Grade.Where(m => (keyStore == null || m.DrugStoreID == keyStore));

            if (keyString != "" && keyStore == null)
            {
                //searchmodel = db.DSA_DrugStore.Where(m => m.Name.Contains(keyString) || m.ResponsiblePerson.Contains(keyString));
                //searchmodel2 = db.DSA_GradeConfig.Where(m => m.GradeContent.Contains(keyString));
                var model = from g in db.DSA_Grade
                            join d in db.DSA_DrugStore on g.DrugStoreID equals d.DrugStoreID
                            join c in db.DSA_GradeConfig on g.AssessmentConfigID equals c.GradeConfigID
                            where
                                (keyStore == null || d.DrugStoreID == keyStore) &&
                                (d.Name.Contains(keyString) || d.Address.Contains(keyString) ||
                                 c.GradeContent.Contains(keyString))
                            select new {g.GradeID, d.Name, g.Grade, g.Remarks, d.ResponsiblePerson, c.GradeContent};

                ViewBag.totalnum = model.Count();
                model = model.OrderBy(m => m.GradeID).Skip(skipnum).Take(numPerPage.Value);
                ViewBag.currentpagenum = model.Count();
                return View(model.ToList());
            }

            ViewBag.totalnum = DSA_grade.Count();
            DSA_grade = DSA_grade.OrderBy(m => m.GradeID).Skip(skipnum).Take(numPerPage.Value);
            ViewBag.currentpagenum = DSA_grade.Count();
            return View(DSA_grade.ToList());
        }

        [HttpGet]
        public ActionResult ConfigList()
        {
            var dc = new yjjDBEntities();
            IQueryable<sys_dict> list = from d in dc.sys_dict
                                        where d.Name == "考核项目"
                                        select d;
            ViewBag.selecttype = list;

            ViewBag.pageNum = 1;
            ViewBag.numPerPage = 20;
            //ViewBag.ProjectClassification = "";
            ViewBag.totalnum = db.DSA_GradeConfig.Count();

            IQueryable<DSA_GradeConfig> model = db.DSA_GradeConfig.OrderBy(m => m.GradeConfigID).Take(20);

            return View(model.ToList());
        }


        [HttpPost]
        public ActionResult ConfigList(int? ProjectClassifications, int? ProjectClassification, int? pageNum,
                                       int? numPerPage)
        {
            var dc = new yjjDBEntities();
            IQueryable<sys_dict> list = from d in dc.sys_dict
                                        where d.Name == "考核项目"
                                        select d;
            ViewBag.selecttype = list;

            if (pageNum == null) pageNum = 1;
            if (numPerPage == null) numPerPage = 20;
            ViewBag.pageNum = pageNum.Value;
            ViewBag.numPerPage = numPerPage.Value;
            int skipnum = (pageNum.Value - 1)*numPerPage.Value;

            ViewBag.ProjectClassification =
                ProjectClassification =
                (ProjectClassification == null && ProjectClassifications != null)
                    ? ProjectClassifications
                    : ProjectClassification;

            IQueryable<DSA_GradeConfig> model =
                db.DSA_GradeConfig.Where(
                    m => ProjectClassification == null || m.ProjectClassification == ProjectClassification);
            ViewBag.totalnum = model.Count();
            model = model.OrderBy(m => m.GradeConfigID).Skip(skipnum).Take(numPerPage.Value);
            return View(model.ToList());
        }

        [HttpGet]
        public ActionResult ConfigAdd()
        {
            var dc = new yjjDBEntities();
            IQueryable<sys_dict> list = from d in dc.sys_dict
                                        where d.Name == "考核项目"
                                        select d;
            ViewBag.selecttype = list;

            return View();
        }

        [HttpPost]
        [ValidateInput(false)] //允许表单提交html<>字段
        public ActionResult ConfigAdd(DSA_GradeConfig g)
        {
            var dc = new yjjDBEntities();

            IQueryable<sys_dict> list = from d in dc.sys_dict
                                        where d.Name == "考核项目"
                                        select d;
            ViewBag.selecttype = list;

            if (ModelState.IsValid)
            {
                g.StandardRule = NoScript(g.StandardRule);
                g.GradeExplanation = NoScript(g.GradeExplanation);

                db.AddToDSA_GradeConfig(g);
                db.SaveChanges();

                var reslut = new
                                 {
                                     statusCode = "200",
                                     message = "添加成功" //,
                                     //navTabId = "page-drugstoremanage",
                                     //callbackType = "closeCurrent"
                                 };
                return Json(reslut);
            }
            else
            {
                var reslut = new
                                 {
                                     statusCode = "300",
                                     message = "添加失败" //,
                                     //navTabId = "page-drugstoremanage",
                                     //callbackType = "closeCurrent"
                                 };
                return Json(reslut);
            }
        }

        //过滤所有html标签
        private string NoHTML(string Htmlstring)
        {
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }

        //过滤<script></script>
        private string NoScript(string Htmlstring)
        {
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            return Htmlstring;
        }

        public ActionResult ConfigContext(int id)
        {
            DSA_GradeConfig model = db.DSA_GradeConfig.Where(m => m.GradeConfigID == id).FirstOrDefault();
            return View(model);
        }
    }
}